package com.wtwd.campus.adapter;

import com.wtwd.campus.common.RespCode;
import com.wtwd.campus.entity.VerifyResult;
import com.wtwd.campus.method.PostPutEncrypt;
import com.wtwd.campus.method.QueryEncrypt;
import com.wtwd.campus.service.RedisService;
import org.apache.commons.lang.StringUtils;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author ldaoliang
 * @Date create in 10:13 2021/7/5
 * @Description
 */
public class CustomerVerifyAdapter extends VerifyAdapter{

    public CustomerVerifyAdapter(HttpServletRequest request) {
        super(request);
        String method = request.getMethod();
        if(method.equalsIgnoreCase(VerifyAdapter.get) || method.equalsIgnoreCase(VerifyAdapter.delete)){
            encryptVerify = new QueryEncrypt();
        }else if(method.equalsIgnoreCase(VerifyAdapter.post) || method.equalsIgnoreCase(VerifyAdapter.put)){
            encryptVerify = new PostPutEncrypt();
        }
    }

    @Override
    public VerifyResult verifyEncrypt(HttpServletRequest request, RedisService redisService) {
        // 开发时放行url
//        String url = request.getRequestURI();
//        if(url.contains("/testPush1")){
//            return null;
//        }
        if(sign.equalsIgnoreCase("123456789")){
            return null;
        }
        if(StringUtils.isBlank(sign) || StringUtils.isBlank(accessToken) || !redisService.hasKey(accessToken)){
            VerifyResult verifyResult = new VerifyResult();
            verifyResult.setResult(false);
            verifyResult.setRespCode(RespCode.TOKEN_UNAUTHORIZED);
        }
        return encryptVerify.verify(sign,accessToken,request,redisService);
    }
}
